﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CrushGame
{
    /// <summary>
    /// Base class for things in the world
    /// </summary>
    public class Thing : IThingInterface
    {
        //Fields from our interface
        bool _isActive;
        Texture2D _sprite;
        Vector2 _location;
        Vector2 _velocity;
        CollisionObject2D _hitbox;

        /// <summary>
        /// Thing's Location in the world
        /// </summary>
        public Vector2 Location
        {
            get { return _location; }
            set { _location = value; }
        }
        /// <summary>
        /// The sprite for the Thing
        /// </summary>
        public Texture2D Sprite
        {
            get { return _sprite; }
        }
        /// <summary>
        /// The Thing's current velocity
        /// </summary>
        public Vector2 Velocity
        {
            get { return _velocity; }
            set { _velocity = value; }
        }
        /// <summary>
        /// Dictates if the Thing needs to be updated
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }
        /// <summary>
        /// Collision object
        /// </summary>
        public CollisionObject2D Hitbox
        {
            get { return _hitbox; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public Thing()
        {
            //Initialize basic data here
            _location = new Vector2(300.0f, 300.0f);
            _velocity = new Vector2(0.0f, 0.0f);
        }

        /// <summary>
        /// Loads this thing's sprite
        /// </summary>
        /// <param name="Content">The Content Manager to load the thing's assets through</param>
        /// <param name="OverrideImage">If a non-standard sprite for this thing-type is needed, specify asset name here</param>
        public virtual void ContentInitialize(ContentManager Content, string OverrideImage = "")
        {
            //Load basic Thing sprite
            if (OverrideImage != "")
            {
                _sprite = Content.Load<Texture2D>(OverrideImage);
            }
            else
            {
                _sprite = Content.Load<Texture2D>("Thing");
            }
            _hitbox = new CollisionObject2D(this);
        }

        /// <summary>
        /// Updates the Thing
        /// </summary>
        /// <param name="DeltaTime">Time elapsed since last frame</param>
        public virtual void Update(float DeltaTime)
        {
            //If we aren't active do nothing
            if (!_isActive)
                return;

            //If we are active, update location by velocity
            _location += _velocity * DeltaTime;
            _hitbox.Update();
        }

        /// <summary>
        /// Shuts down and cleans up components or connections
        /// </summary>
        public virtual void Shutdown()
        {
            //Nothing to shut down yet
        }

        /// <summary>
        /// Draw the thing to the screen at our location
        /// </summary>
        /// <param name="Batch">The batch to draw into</param>
        public virtual void Draw(SpriteBatch Batch)
        {
            Batch.Draw(_sprite, _location, Color.White);
        }

    }
}
